package com.wsdc.encrption;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.View;
import android.widget.TextView;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class RSAActivity extends AppCompatActivity {
    private TextView mTextView;

    //公钥
    private PublicKey mPublicKey;

    //私钥    非常重要
    private PrivateKey mPrivateKey;

    private String content = "百家争鸣，单刀赴会";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rsa);

        initView();

        new Thread(new Runnable() {

            @Override
            public void run() {
                //获取公钥私钥对象
                try {
                    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");

                    //初始化   指定密码的长度 位
                    generator.initialize(1024);

                    //获取公钥和私钥
                    KeyPair keyPair = generator.generateKeyPair();

                    mPrivateKey = keyPair.getPrivate();

                    mPublicKey = keyPair.getPublic();

                    //密码的具体内容
                    byte[] encoded = mPrivateKey.getEncoded();

                    byte[] encoded1 = mPrivateKey.getEncoded();

                    //将数据解析给我们看  编码    计算机识别  解码
                    final byte[] privateKeyContent = Base64.encode(encoded, Base64.DEFAULT);
                    final byte[] publicKeyContent = Base64.encode(encoded1, Base64.DEFAULT);

                    mTextView.post(new Runnable() {
                        @Override
                        public void run() {
                            mTextView.setText("公钥 "+new String(publicKeyContent)+"\n"
                                    +"私钥"+new String(privateKeyContent));
                        }
                    });


                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    private void initView() {
        mTextView = (TextView) findViewById(R.id.main_text1);
    }

    //使用公钥和私钥
    public void click(View view) {
        //用私钥加密文件
        //用公钥解密文件

        switch (view.getId()){
            case R.id.main_private:
                try {
                    Cipher cipher = Cipher.getInstance("RSA");

                    if(mPrivateKey != null){
                        cipher.init(Cipher.ENCRYPT_MODE,mPrivateKey);
                    }

                    byte[] bytes = cipher.doFinal(content.getBytes());

                    byte[] encode = Base64.encode(bytes, Base64.DEFAULT);

                    mTextView.setText(new String(encode));

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }

                break;

            case R.id.main_public:
                try {
                    Cipher cipher = Cipher.getInstance("RSA");

                    if(mPublicKey != null){
                        cipher.init(Cipher.DECRYPT_MODE,mPublicKey);
                    }
                    String temp = mTextView.getText().toString().trim();

                    byte[] encode1 = Base64.decode(temp.getBytes(), Base64.DEFAULT);

                    byte[] bytes1 = cipher.doFinal(encode1);


                    mTextView.setText(new String(bytes1));

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    e.printStackTrace();
                }


                break;
        }
    }
}
